import {Component, Input, OnInit} from '@angular/core';
import {navigation} from 'app/navigation/navigation';
import {Router} from '@angular/router';
import {Title} from '@angular/platform-browser';

@Component({
    selector: 'breadcrumb',
    templateUrl: './breadcrumb.component.html',
    styleUrls: ['./breadcrumb.component.scss']
})
export class BreadcrumbComponent implements OnInit {
    @Input() type: string;

    public navigation: any;
    breadcrumbList: Array<any> = [];
    public navigationList: any;

    constructor(private _router: Router, private titleService: Title) {
        this.navigation = navigation;
        this.setBreadcrumb();
        this.type = 'theme2';
    }

    ngOnInit(): void {
    }

    setBreadcrumb(): void {
        let routerUrl: string;
        this._router.events.subscribe((router: any) => {
            routerUrl = router.urlAfterRedirects;
            if (routerUrl && typeof routerUrl === 'string') {
                this.breadcrumbList.length = 0;
                const activeLink = router.url;
                this.filterNavigation(activeLink);
            }
        });
    }

    filterNavigation(activeLink): void {
        let result: any;
        let title = 'Welcome';
        this.navigation.forEach((a) => {
            if (a.type === 'item' && 'url' in a && a.url === activeLink) {
                result = [{
                    'url': ('url' in a) ? a.url : false,
                    'title': a.title,
                    'breadcrumbs': ('breadcrumbs' in a) ? a.breadcrumbs : true,
                    'type': a.type
                }];
                title = a.title;
            } else {
                if (a.type === 'group' && 'children' in a) {
                    a.children.forEach((b) => {
                        if (b.type === 'item' && 'url' in b && b.url === activeLink) {
                            result = [
                                {
                                    'url': ('url' in a) ? a.url : false,
                                    'title': a.title,
                                    'breadcrumbs': ('breadcrumbs' in a) ? a.breadcrumbs : true,
                                    'type': a.type
                                },
                                {
                                    'url': ('url' in b) ? b.url : false,
                                    'title': b.title,
                                    'breadcrumbs': ('breadcrumbs' in b) ? b.breadcrumbs : true,
                                    'type': b.type
                                }
                            ];
                            title = b.title;
                        } else {
                            if (b.type === 'collapsible' && 'children' in b) {
                                b.children.forEach((c) => {
                                    if (c.type === 'item' && 'url' in c && c.url === activeLink) {
                                        result = [
                                            {
                                                'url': ('url' in a) ? a.url : false,
                                                'title': a.title,
                                                'breadcrumbs': ('breadcrumbs' in a) ? a.breadcrumbs : true,
                                                'type': a.type
                                            },
                                            {
                                                'url': ('url' in b) ? b.url : false,
                                                'title': b.title,
                                                'breadcrumbs': ('breadcrumbs' in b) ? b.breadcrumbs : true,
                                                'type': b.type
                                            },
                                            {
                                                'url': ('url' in c) ? c.url : false,
                                                'title': c.title,
                                                'breadcrumbs': ('breadcrumbs' in c) ? c.breadcrumbs : true,
                                                'type': c.type
                                            }
                                        ];
                                        title = c.title;
                                    } else {
                                        if (c.type === 'collapsible' && 'children' in c) {
                                            c.children.forEach((d) => {
                                                if (d.type === 'item' && 'url' in d && d.url === activeLink) {
                                                    result = [
                                                        {
                                                            'url': ('url' in a) ? a.url : false,
                                                            'title': a.title,
                                                            'breadcrumbs': ('breadcrumbs' in a) ? a.breadcrumbs : true,
                                                            'type': a.type
                                                        },
                                                        {
                                                            'url': ('url' in b) ? b.url : false,
                                                            'title': b.title,
                                                            'breadcrumbs': ('breadcrumbs' in b) ? b.breadcrumbs : true,
                                                            'type': b.type
                                                        },
                                                        {
                                                            'url': ('url' in c) ? c.url : false,
                                                            'title': c.title,
                                                            'breadcrumbs': ('breadcrumbs' in c) ? c.breadcrumbs : true,
                                                            'type': c.type
                                                        },
                                                        {
                                                            'url': ('url' in d) ? d.url : false,
                                                            'title': d.title,
                                                            'breadcrumbs': ('breadcrumbs' in d) ? d.breadcrumbs : true,
                                                            'type': d.type
                                                        }
                                                    ];
                                                    title = c.title;
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
        this.navigationList = result;
        this.titleService.setTitle(title + ' | 后台管理系统');
    }

}
